﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace SeleniumRecorder
{
    /// <summary>
    /// Interaction logic for GenerateCode.xaml
    /// </summary>
    public partial class GenerateCode : Window
    {
        Dictionary<string, ControlDataStructure> dcs = null;
        public string windowTitle;
        public string url="";
        public GenerateCode(Dictionary<string, ControlDataStructure> dcs, string title,string url)
        {
            InitializeComponent();
            this.Topmost = true;
            this.dcs = dcs;
            this.windowTitle = title;
            this.url = url;
        }

        private void btnClose_Click_Close(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_Cancel(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void Button_Click_Generate(object sender, RoutedEventArgs e)
        {

            string nameSpace = this.NameSpaceText.Text;
            string className = this.ClassNameText.Text;
            string product = ((System.Windows.Controls.ContentControl)(ProductCombo.SelectedItem)).Content.ToString();

            if (product == null || product == "")
            {
                MessageBox.Show("Please select product.");
                return;
            }

            SeleniumRecorder.HelperClass.GenerateCode.GenerateCustomCode(dcs, windowTitle, product, nameSpace, className);
            CreateXML(dcs, className);
            this.Close();
        }

        public void CreateXML(Dictionary<string, ControlDataStructure> proccontrol, string className)
        {
            XmlTextWriter writer = new XmlTextWriter(@" C:\POMs\" + className + ".xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("Nodes"); 
            writer.WriteStartElement("URL");
            writer.WriteString(url);
            writer.WriteEndElement();
            foreach (var d in proccontrol)
            {
                createNode(d, writer);
            }

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MessageBox.Show("XML File created ! ");
        }

        private void createNode(KeyValuePair<string, ControlDataStructure> dc, XmlTextWriter writer)
        {
            writer.WriteStartElement("TreeNode");

            writer.WriteAttributeString("Key", dc.Value.FieldName);
            writer.WriteAttributeString("CSSSelectorValue", dc.Value.CssSelectorValue);
            writer.WriteAttributeString("FieldName", dc.Value.FieldName);
            writer.WriteAttributeString("IDValue", dc.Value.ID);
            writer.WriteAttributeString("NameValue", dc.Value.Name);
            writer.WriteAttributeString("XPathValue", dc.Value.XpathValue);
            writer.WriteAttributeString("ClassName", dc.Value.ClassName);
            writer.WriteAttributeString("parent", dc.Value.parent);
            writer.WriteAttributeString("SerachBy", dc.Value.SerachBy.ToString());
            writer.WriteAttributeString("SearchValue", dc.Value.SearchValue);
            writer.WriteAttributeString("IsParent", dc.Value.isParent.ToString());
            writer.WriteAttributeString("TagName", dc.Value.TagName);
            writer.WriteAttributeString("Partiallink", dc.Value.PartialLink);
            writer.WriteAttributeString("LinkText", dc.Value.LinkText); 
            writer.WriteStartElement("CSSAttributes");

            foreach (var v in dc.Value.attributes)
            {
                writer.WriteStartElement("Attribute");
                writer.WriteAttributeString("Key", v.Key);
                writer.WriteString(v.Value);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
